$.ajaxSetup({
	headers:{
		'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	}
});

$(".post-audit").click(function(event){
	target = $(event.target);
	var post_id = target.attr('post-id');
	var status = target.attr('post-action-status');

	$.ajax({
		url:"/admin/posts/" + post_id + "/status",
		method:"POST",
		data:{"status": status},
		dataType:"json",
		success:function (data) {
			if (data.error != 0) {
				alert(data.msg);
				return;
			}
			target.parent().parent().remove();
		}
	});
});

/**处理弹框表单提交*/
function dialog_from(url,title){
    $.ajax({type: "GET", url:url, dataType:'text',
        success: function (text) {
            bootbox.dialog({
                message: text,
                title: title?title:'',
                buttons: {
                    Cancel: {
                        label: "取消",
                        className: "btn-default",
                    }
                    , OK: {
                        label: "确认",
                        className: "btn-primary",
                        callback: function () {
                            var data = window.$("#myform").submit();
                        }
                    }
                }
            });
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            bootbox.alert(HttpRequest.responseText)
        }
    });
}

/**处理弹框表单提交*/
function dialog_group(url,title){
    $.ajax({type: "GET", url:url, dataType:'text',
        success: function (text) {
            bootbox.dialog({
                message: text,
                title: title?title:'',
                buttons: {
                    Cancel: {
                        label: "确定",
                        className: "btn-primary",
                    }
                }
            });
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            bootbox.alert(HttpRequest.responseText)
        }
    });
}

/*处理带参数的表单提交*/
function dialog_params(url,title){
    $.ajax({type: "GET", url:url, dataType:'text',
        success: function (text) {
            bootbox.dialog({
                message: text,
                title: title?title:'',
                buttons: {
                    Cancel: {
                        label: "取消",
                        className: "btn-default",
                    }
                    , OK: {
                        label: "确认",
                        className: "btn-primary",
                        callback: function () {
                            var data = window.$("#myform").serialize();
                            // 将表单数据ajax提交验证
                            $.ajax({type: "POST",dataType:"json", url: url, data: data,
                                success: function(data) {
                                    //验证成功
                                    if (data.flag) {
                                        bootbox.hideAll();
                                        bootbox.alert(data.msg,function(){
                                            window.location.reload();
                                        });
                                    } else {
                                        $.each(data,function(k,d){
                                            var str = '';
                                            $.each(d,function(ks,ds){
                                                str+= ds;
                                            })
                                            window.$("#myform").find('#err_'+k).html(str)
                                        })
                                        return false;
                                    }
                                },
                                error: function(HttpRequest, ajaxOptions, thrownError) {
                                    bootbox.alert(HttpRequest.responseText)
                                }
                            });
                            return false;
                        }
                    }
                }
            });
        },
        error: function(HttpRequest, ajaxOptions, thrownError) {
            bootbox.alert(HttpRequest.responseText)
        }
    });
}

/**
 * [trim 去除字符串两侧指定字符串]
 * @param  {[type]} char [description]
 * @param  {[type]} type [description]
 * @return {[type]}      [description]
 */
String.prototype.trim = function (char, type) {
  if (char) {
    if (type == 'left') {
      return this.replace(new RegExp('^\\'+char+'+', 'g'), '');
    } else if (type == 'right') {
      return this.replace(new RegExp('\\'+char+'+$', 'g'), '');
    }
    return this.replace(new RegExp('^\\'+char+'+|\\'+char+'+$', 'g'), '');
  }
  return this.replace(/^\s+|\s+$/g, '');
};

/**
 * [indexOf 获取指定元素的下标]
 * @param  {[type]} val [description]
 * @return {[type]}     [description]
 */
Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};

/**
 * [remove 删除指定元素]
 * @param  {[type]} val [description]
 * @return {[type]}     [description]
 */
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};